<script setup>
	import FirstTitle from "@comp/FirstTitle";
	import {
		Editor,
		Toolbar
	} from "@wangeditor/editor-for-vue";
	import {
		onMounted
	} from "vue";
	import {
		searchDataZhbz
	} from "@/api/search";
	import {
		addOrUpdateZhbz
	} from "@/api/addOrUpdate";

	import {
		postTableData
	} from "@/api/table";


	const city = window.globalObj.name;
	const editorRef = shallowRef();
	const editorRef1 = shallowRef();
	const editorRef2 = shallowRef();
	const editorRef3 = shallowRef();

	const toolbarConfig = {};
	const toolbarConfig1 = {};
	const toolbarConfig2 = {};
	const toolbarConfig3 = {};

	const editorConfig = {
		placeholder: "请输入内容..."
	};
	const mode = ref("default");
	const mode1 = ref("default");
	const mode2 = ref("default");
	const mode3 = ref("default");

	// 加载html
	const initHtml = () => {
		searchDataZhbz({
			m: "任务需求-物资经费保障需求"
		}).then((res) => {
			const {
				data
			} = res
			valueHtml.value = data.find((x) => x.k === "生活物资保障需求")?.v || "";
			valueHtml1.value = data.find((x) => x.k === "救灾物资保障需求")?.v || "";
			valueHtml2.value = data.find((x) => x.k === "工程物资")?.v || "";
			valueHtml3.value = data.find((x) => x.k === "经费保障需求")?.v || "";
		})
	};

	const handleCreated = (editor) => {
		editorRef.value = editor; // 记录 editor 实例，重要！
	};
	const handleCreated1 = (editor) => {
		editorRef1.value = editor; // 记录 editor 实例，重要！
	};
	const handleCreated2 = (editor) => {
		editorRef2.value = editor; // 记录 editor 实例，重要！
	};
	const handleCreated3 = (editor) => {
		editorRef3.value = editor; // 记录 editor 实例，重要！
	};

	const valueHtml = ref(
		"按城市人口XX%（XX万人）计算，米、面、食用油、食盐、饮用水需保障XX天，即米XX吨，面XX吨、食用油XX吨、食盐XX吨、饮用水XX万L；肉、蔬菜、水果需保障XX天，即肉XX吨，蔬菜XX吨，水果XX吨。"
	);

	const valueHtml1 = ref(
		"按城市人口XX%（XX万人）计算，需保障帐篷、应急灯XX个，净水器、简易厕所、沐浴房XX个，应急食品XX万份，饮用水XX万瓶，折叠床、棉被、毛巾被、棉衣、秋衣等XX万件。"
	)
	const valueHtml2 = ref(
		"保障战储标准：按照抢修重要目标使用常规混凝土XX万m³概算各类工程物资保障战储数量。"
	)
	const valueHtml3 = ref(
		"生活物资保障经费：按每人每天XX元标准，城市人口XX万人，保障XX天，总计XX亿元预算。<br/>抢险救灾行动保障经费：按每人XX元计算，共XX亿元预算。"
	)

	const saveProgramme = () => {
		ElMessageBox.confirm(`确定要保存吗?`, "警告", {
			confirmButtonText: "确定",
			cancelButtonText: "取消",
			type: "warning",
			customClass: "message-box",
		}).then(() => {
			addOrUpdateZhbz({
					m: "任务需求-物资经费保障需求",
					data: [{
							k: "生活物资保障需求",
							v: valueHtml.value,
						},
						{
							k: "救灾物资保障需求",
							v: valueHtml1.value,
						},
						{
							k: "工程物资",
							v: valueHtml2.value,
						},
						{
							k: "经费保障需求",
							v: valueHtml3.value,
						}
					],
				})
				.then((result) => {
					if (result.code === 200) {
						ElMessage.success("操作成功");
						initHtml();
					} else {
						ElMessage.error(result.msg);
					}
				})
				.catch(() => {});
		});
	}

	onMounted(() => {
		initHtml();
	});
	onBeforeUnmount(() => {
		const editor = editorRef.value;
		const editor1 = editorRef1.value;
		const editor2 = editorRef2.value;
		const editor3 = editorRef3.value;
		if (!editor) return;
		if (!editor1) return;
		if (!editor2) return;
		if (!editor3) return;
		editor.destroy();
		editor1.destroy();
		editor2.destroy();
		editor3.destroy();
	});
	// 表头单元格样式
	const headerCellStyle = () => {
		return {
			backgroundColor: "#f5f7fa",
			color: "rgba(0,0,0,0.85)",
			fontSize: "14px",
			height: "54px",
		};
	};
	// 单元格样式
	const cellStyle = () => {
		return {
			color: "rgba(0,0,0,0.65)",
			fontSize: "14px",
			lineHeight: "22px",
		};
	};

	const tableData = ref([])
	const getList = () => {
		postTableData({
			docType: "comprehensiveSupport",
			method: "zhiyuangongchengwuzizhonglei",
		}).then(result => {
			if (result.code === 200) {
				const halfLength = Math.floor(result.data.length / 2);
				for (let i = 0; i < halfLength; i++) {
					result.data[i].correspondingId = result.data[i + halfLength].id
					result.data[i].correspondingName = result.data[i + halfLength].name
					delete result.data[i + halfLength].id;
					delete result.data[i + halfLength].name;
				}
				result.data.splice(halfLength)
				tableData.value = result.data
			}
		})
	}



	getList()
</script>
<template>
	<div class="">
		<FirstTitle name="生活物资保障需求" />
		<div style="border: 1px solid #ccc" class="top-15">
			<Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig"
				:mode="mode" />
			<Editor style="height: 500px; overflow-y: hidden" v-model="valueHtml" :defaultConfig="editorConfig"
				:mode="mode" @onCreated="handleCreated" />
		</div>

		<FirstTitle name="救灾物资保障需求" class="top-15" />
		<div style="border: 1px solid #ccc" class="top-15">
			<Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef1" :defaultConfig="toolbarConfig1"
				:mode="mode1" />
			<Editor style="height: 500px; overflow-y: hidden" v-model="valueHtml1" :defaultConfig="editorConfig"
				:mode="mode1" @onCreated="handleCreated1" />
		</div>

		<FirstTitle name="工程物资" class="top-15" />
		<div style="border: 1px solid #ccc" class="top-15">
			<Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef2" :defaultConfig="toolbarConfig2"
				:mode="mode2" />
			<Editor style="height: 500px; overflow-y: hidden" v-model="valueHtml2" :defaultConfig="editorConfig"
				:mode="mode2" @onCreated="handleCreated2" />
		</div>
		<div class="top-15 text-center">
			{{city}}工程物资战储种类参考清单
		</div>
		<el-table id="table" :data="tableData" class="top-15" style="width: 100%" :header-cell-style="headerCellStyle"
			:cell-style="cellStyle" border>
			<el-table-column prop="id" label="序号" align="center" />
			<el-table-column prop="name" label="名称" />
			<el-table-column prop="correspondingId" label="序号" align="center" />
			<el-table-column prop="correspondingName" label="名称" />
		</el-table>



		<FirstTitle name="经费保障需求" class="top-15" />
		<div style="border: 1px solid #ccc" class="top-15">
			<Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef3" :defaultConfig="toolbarConfig3"
				:mode="mode3" />
			<Editor style="height: 500px; overflow-y: hidden" v-model="valueHtml3" :defaultConfig="editorConfig"
				:mode="mode3" @onCreated="handleCreated3" />
		</div>






		<div class="flex-justify-end top-20">
			<el-button type="primary" class="save-btn" @click="saveProgramme">保存</el-button>
		</div>
	</div>
</template>
<style scoped lang="scss"></style>